package com.renwei.yunlong.activity.me;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.FormatUtil;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.lang.ref.WeakReference;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ChangeAccountActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.et_pwd)
    EditText etPwd;
    @Bind(R.id.et_mobile)
    EditText etMobile;
    @Bind(R.id.et_code)
    EditText etCode;
    @Bind(R.id.bt_code)
    Button btCode;

    private PasswordHandler myHandler = new PasswordHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_account);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        initView();
    }

    private void initView() {
        simpleTileView.setTitle("更换手机");
        simpleTileView.setRightText("提交", this);

        btCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_parent:
                upMobile();
                break;
            case R.id.bt_code:
                getCode();
                break;
        }
    }

    private void upMobile() {
        String phone = etMobile.getText().toString().trim();
        String pwd = etPwd.getText().toString().trim();
        String code = etCode.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            showCenterInfoMsg("请输入密码");
            return;
        }
        if (!FormatUtil.isMobileNO(phone)) {
            showCenterInfoMsg("请输入正确的手机号码");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            showCenterInfoMsg("请输入验证码");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
        } else if ("2".equals(companyType)) {
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
        }
        map.put("mobilePhone", phone);
        map.put("vercode", code);
        map.put("userPwd", pwd);
        ServiceRequestManager.getManager().changeMoblie(this, JsonMapUtil.mapToJson(map), this);
    }

    private void getCode() {
        String phone = etMobile.getText().toString().trim();
        String pwd = etPwd.getText().toString().trim();
        if (!FormatUtil.isMobileNO(phone)) {
            showCenterInfoMsg("请输入正确的手机号码");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
        } else if ("2".equals(companyType)) {
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
        }
        map.put("mobilePhone", phone);
        map.put("userPwd", pwd);
        ServiceRequestManager.getManager().getChangeMoblieCode(this, JsonMapUtil.mapToJson(map), this);

    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1: //验证码
                CommonBean commonBean = new Gson().fromJson(result, CommonBean.class);
                int commonCode = commonBean.getMessage().getCode();
                if (commonCode == 200) {
                    // 开始倒计时
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 59; i >= 0; i--) {
                                Message msg = myHandler.obtainMessage();
                                msg.arg1 = i;
                                myHandler.sendMessage(msg);
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }).start();
                } else if (commonCode == 1033) {
                    showCenterInfoMsg("手机号码已注册");
                } else if (commonCode == 1008) {
                    showCenterInfoMsg("无权限访问");
                } else if (commonCode == 1002) {
                    showCenterInfoMsg("密码错误");
                } else if (commonCode == 202) {
                    showCenterInfoMsg("数据传输错误");
                }
                break;
            case 2: // 提交
                CommonBean bean = new Gson().fromJson(result, CommonBean.class);
                int code = bean.getMessage().getCode();
                if (code == 200) {
                    showCenterSuccessMsg("修改成功");
                    exitApp();
                } else if (code == 1033) {
                    showCenterInfoMsg("手机号码已注册");
                } else if (code == 1111) {
                    showCenterInfoMsg("验证码错误");
                } else if (code == 1008) {
                    showCenterInfoMsg("无权限访问");
                } else if (code == 1002) {
                    showCenterInfoMsg("密码错误");
                } else if (code == 202) {
                    showCenterInfoMsg("数据传输错误");
                }
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
    }

    /**
     * 使用Handler
     * <p>
     * 静态内部类：避免内存泄漏
     */
    private class PasswordHandler extends Handler {
        private final WeakReference<ChangeAccountActivity> weakReference;

        public PasswordHandler(ChangeAccountActivity activity) {
            weakReference = new WeakReference<ChangeAccountActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ChangeAccountActivity activity = weakReference.get();
            if (activity != null) {
                switch (msg.what) {
                    case 0:
                        try {
                            if (msg.arg1 == 0) {
                                btCode.setText("获取验证码");
                                btCode.setBackgroundColor(Color.parseColor("#689df8"));
                                btCode.setTextColor(Color.parseColor("#FFFFFF"));
                                btCode.setClickable(true);
                            } else {
                                btCode.setText("(" + msg.arg1 + ")秒");
                                btCode.setBackgroundColor(Color.parseColor("#b8bfcc"));
                                btCode.setClickable(false);
                            }
                        } catch (Exception e) {
                            LogUtil.e(e.getMessage());
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
